﻿using System.Data;
using System.Data.SqlClient;

namespace DAO
{
    /// <summary>
    /// Summary description for DDatHangDAO
    /// </summary>
    public  class DDatHangDAO
    {
        public  DDatHangDAO()
        {
            //
            // TODO: Add constructor logic here
            //
        }
        #region lay thong tin
        public static DataTable GetInvoiceDetails(DDatHangDTO ddhDTO)
        {
            string sQuery = "select * InvoiceDetails where Codebill =" + ddhDTO.Codebill;
            SqlConnection con = DataProvider.TaoKetNoi();
            DataTable dt = DataProvider.LayDataTable(sQuery, con);
            DataProvider.DongKetNoi(con);
            return dt;
        }
        public static string GetCodeBill()
        {
            string sQuery = "select top 1 Codebill from Invoice order by Codebill desc";
            SqlConnection con = DataProvider.TaoKetNoi();
            DataTable dt = DataProvider.LayDataTable(sQuery, con);
            DataProvider.DongKetNoi(con);
            string kq = dt.Rows[0][0].ToString();
            return kq;
        }
        public static DataTable TakingOrdersListings()
        {
            string sQuery = "select * from Invoice order by Codebill asc";
            SqlConnection con = DataProvider.TaoKetNoi();
            DataTable dt = DataProvider.LayDataTable(sQuery, con);
            DataProvider.DongKetNoi(con);
            return dt;
        }
        #endregion
        #region them thong tin
        public static bool AddOrders(DDatHangDTO ddhDTO)
        {
            string sQuery = "insert into Invoice(UserName,DeliveryLocations,PhoneNumber,RecipientName,Deliverydate,Total,EnterInvoiceDate) values('"
                            + ddhDTO.Username + "','" + ddhDTO.address + "','" + ddhDTO.PhoneNumber + "','" + ddhDTO.RecipientName + "','"
                            + ddhDTO.Deliverydate + "'," + ddhDTO.Total + ",'" + ddhDTO.EnterInvoiceDate + "')";
            SqlConnection con = DataProvider.TaoKetNoi();
            bool kq = DataProvider.ExecuteNonQuery(sQuery, con);
            DataProvider.DongKetNoi(con);
            return kq;
        }
        public static void AddInvoiceDetails(DDatHangDTO ddhDTO)
        {
            string sQuery = "insert into InvoiceDetails(Codebill,ProductCode,UnitPrice,Number) values(" + ddhDTO.Codebill
                            + "," + ddhDTO.ProductCode + "," + ddhDTO.UnitPriceSP + "," + ddhDTO.NumberSP + ")";
            SqlConnection con = DataProvider.TaoKetNoi();
            DataProvider.ExecuteNonQuery(sQuery, con);
            DataProvider.DongKetNoi(con);
        }

        public static void EditOrders(DDatHangDTO ddhDTO)
        {
            string sQuery = "update Invoice set UserName = '" + ddhDTO.Username + "',DeliveryLocations ='"
                            + ddhDTO.address + "',PhoneNumber ='" + ddhDTO.PhoneNumber + "',RecipientName='" + ddhDTO.RecipientName + "',Deliverydate='"
                            + ddhDTO.Newdelivery + "',Total=" + ddhDTO.Total + ",EnterInvoiceDate='" + ddhDTO.NewOrder + "' where Codebill =" + ddhDTO.Codebill;
            SqlConnection con = DataProvider.TaoKetNoi();
            DataProvider.ExecuteNonQuery(sQuery, con);
            DataProvider.DongKetNoi(con);
        }
        public static void DeletingOrders(DDatHangDTO ddhDTO)
        {
            string sQuery = "delete from Invoice where Codebill =" + ddhDTO.Codebill;
            SqlConnection con = DataProvider.TaoKetNoi();
            DataProvider.ExecuteNonQuery(sQuery, con);
            DataProvider.DongKetNoi(con);
        }
  
        #endregion
    }
}